/**
 *  *2010-5-28 上午12:05:57  add 
 */
package com.base.myproject.client.busi.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BaseStore.EditDisableType;
import com.base.myproject.client.busi.Editdisable;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.ui.BaseGridPanel;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Widget;

/**
 * code 代码开头 以A开头充值卡
 * S开头系统要求。C开头次卡。系统有调用
 * 充值和消费都有调用
 * @author Administrator
 *2010-5-28
 * javazgw@gmail.com
 */
public class ServiceAccSetting extends BaseJDBCPanel{
	BaseStore<BusiObject> store = null;
	public ServiceAccSetting()
	{
		super();
		init();
	}
	
	protected void init()
	{
		
		this.setWidth("1024px");
		
		this.setAutoHeight(true);
		
		
		this.add(initWidgetx());
		
		registerMainStore(store);
		registerStore(store);
		
	}
	public Widget initWidgetx()
	{
		
		return createGrid();
		
	}
	
	BaseStore<BusiObject> getStore(){
	if(store==null) 
	{
		createStore();
	}
	return store;}
	BaseStore<BusiObject> createStore(){
	ModelType type = new ModelType();
	type.setRoot("records");
	type.setRecordName("record");
	type.setTotalName("totle");
	type.addField("icode");
	type.getField("icode").setType(Long.class);
	type.addField("code");
	type.getField("code").setType(String.class);
	type.addField("name");
	type.getField("name").setType(String.class);
	type.addField("amount");
	type.getField("amount").setType(Double.class);
	type.addField("s_discount");
	type.getField("s_discount").setType(Double.class);
	type.addField("g_discount");
	type.getField("g_discount").setType(Double.class);
	
	type.addField("startdate");
	type.getField("startdate").setType(Date.class);
	type.addField("enddate");
	type.getField("enddate").setType(Date.class);
	type.addField("status");
	type.getField("status").setType(String.class);
	type.addField("_v_count");
	type.getField("_v_count").setType(Integer.class);
	StoreInfo info = new StoreInfo();
	info.setTablename("s_account_setting");
	info.setKeyname(new String[] { "icode"});
	info.setSql("select icode,code,name,amount,s_discount,g_discount,startdate,enddate,status,(select case when count(*) >0 then 1 else count(*) end as _v_count from s_service_finance where s_service_finance.accountcode =  s_account_setting.code) as _v_count from s_account_setting");
	//info.setParent(mainstore);// 关联主表
	//info.setLinkparentcolumns(new String[] { "icode" }); // 关联主表字段
	//info.setLinkparent_chile_columns(new String[] { "upicode" });// 关联主表字段
	  store = new BaseStore<BusiObject>(type, info);
	  
	  store.addEditdisableItem(new Editdisable("code","S",EditDisableType.IN));
	  //store.addEditdisableItem(new Editdisable("_v_count","1",EditDisableType.EQUALS));
	 return store;}
	

	BaseGridPanel grid=null;
	BaseGridPanel createGrid(){
	CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
	List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
	columns.add(cbs.getColumn());

//	ColumnConfig C1 = new ColumnConfig("icode", "icode", 80);
//	columns.add(C1);
	ColumnConfig C2 = new ColumnConfig("code", "代码", 80);
	C2.setEditor(new CellEditor(new TextField<String>()));
	columns.add(C2);
	ColumnConfig C3 = new ColumnConfig("name", "名称", 120);
	C3.setEditor(new CellEditor(new TextField<String>()));
	columns.add(C3);
	ColumnConfig C4 = new ColumnConfig("amount", "< 金额/次卡（次）", 80);
	C4.setEditor(new CellEditor(new NumberField()));
	columns.add(C4);
	ColumnConfig C5 = new ColumnConfig("s_discount", "服务折扣", 80);
	C5.setEditor(new CellEditor(new NumberField()));
	columns.add(C5);
	
	ColumnConfig C10 = new ColumnConfig("g_discount", "服务折扣", 80);
	C10.setEditor(new CellEditor(new NumberField()));
	columns.add(C10);
	
	DateField C_data6 = new DateField(); 
	C_data6.getPropertyEditor().setFormat( 
			DateTimeFormat.getFormat("yyyy-MM-dd"));
	
	ColumnConfig C6 = new ColumnConfig("startdate", "开始日期", 80);
	
	 C6.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
	 C6.setEditor(new CellEditor(C_data6));
	columns.add(C6);
	
	
	
	DateField C_data7 = new DateField(); 
	C_data7.getPropertyEditor().setFormat( 
			DateTimeFormat.getFormat("yyyy-MM-dd"));
	ColumnConfig C7 = new ColumnConfig("enddate", "截止日期", 80);
	 C7.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
	 C7.setEditor(new CellEditor(C_data7));
	columns.add(C7);
	
	
	BaseComboBoxForm<BaseModelData> bsb2 = new BaseComboBoxForm<BaseModelData>("code","name","dictionary"," where type='service_acc_seting'",true);
	ColumnConfig C8 = new ColumnConfig("status", "类型", 140);
	C8.setEditor(bsb2.getCelEditor());
	C8.setRenderer(bsb2);
	columns.add(C8);


	ColumnModel cm = new ColumnModel(columns);
	grid = new BaseGridPanel(getStore(), cm,true,true){
		
		protected void delrecord()
		{
			if(bg.getValue("_v_count")!=null && bg.getValue("_v_count").equals(1))
			{
				MessageBox.alert("提示", "此账户已经存在充值记录，不允许被删除。", null);
				
				return;
			}
				bg.deleterecord();
			
		}
	};
	cbs.setSelectionMode(SelectionMode.SINGLE);
	grid.getBg().setSelectionModel(cbs);
	grid.getBg().setHeight("400");
	grid.getBg().addListener(Events.ValidateEdit, getListener());
	return grid ;}
	

	int sync = 0;                                                      
	Listener<BaseEvent> defaultlistener;                               
                                                                     
	Listener<BaseEvent> getListener() {                                
		if(defaultlistener!=null)                                        
		{                                                                
			return defaultlistener;                                        
		}                                                                
                                                                     
		defaultlistener = new Listener<BaseEvent>() {                    
			public void handleEvent(BaseEvent be) {                        
				sync++;                                                      
				try {                                                        
					if (sync == 1) {                                           
						if (be.getType() == Events.Select) {                     
							if (be.getSource().equals("")) {                            
                                                                     
							}                                                      
						} else if (be.getType() == Events.Change) {              
							if (be.getSource().equals("")) {         
                                                                     
							}                                                      
						} else if (be.getType() == Events.KeyUp) {               
							if (be instanceof ComponentEvent) {                    
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{                                                    
								}                                                    
							}                                                      
						}    
						else if (be.getType() == Events.ValidateEdit) {
							@SuppressWarnings("unchecked")
							final GridEvent<BusiObject> be2 = (GridEvent<BusiObject>)be;
//							if (be2.getProperty().equals("code") ||be2.getProperty().equals("s_discount")||be2.getProperty().equals("name")
//									||be2.getProperty().equals("amount")
//									||be2.getProperty().equals("s_discount")
//									||be2.getProperty().equals("s_discount")) {
//								
//							}
							if (be2.getProperty().equals("code"))
							{
								calIfCancel(be2);
							
							}
						
					}   
					}
				}
				 finally {                                                  
					sync--;                                                    
				}                                                            
			}                                                              
                                                                     
		};                                                               
		return defaultlistener;                                          
	}                                                                  

	void calIfCancel(GridEvent<BusiObject> be)
	{
		
		if(be.getModel().get("_v_count")!=null && be.getModel().get("_v_count").toString().equals("1"))
		{
			be.setCancelled(true);
		}
//		else if(be.getModel().get("code")!=null && be.getModel().get("code").toString().startsWith("S"))
//		{
//			be.setCancelled(true);
//		} 
		
			
			
	}
	
}
